# coding: utf-8
__author__ = 'j.tommy'
import markdown
from markdown.extensions.wikilinks import WikiLinkExtension


def encode(html):
    if html is not None:
        html = html.replace('<', '&lt;')
        html = html.replace('>', '&gt;')
        html = html.replace('"','&quot;')
        html = html.replace('\'','&#39;')
        return html

    return None


def decode(html):
    if html is not None:
        html = html.replace('&lt;', '<')
        html = html.replace('&gt;', '>')
        html = html.replace('&quot;', '"')
        html = html.replace('&#39;', '\'')
        return html
    return None


def markdown_to_html(text):
    """
    markdown转HTML（注意：这里只是演示python markdown插件的用法。实际使用页面可以使用markdown相关的js插件来实现。）
    :return:
    """
    # html = markdown.markdown(text)
    html = ''
    if text is not None:
        html = markdown.markdown(text, output_format='html5', \
                                 extensions=['markdown.extensions.toc', \
                                             WikiLinkExtension(base_url='https://en.wikipedia.org/wiki/', \
                                                               end_url='#Hyperlinks_in_wikis'), \
                                             'markdown.extensions.sane_lists', \
                                             'markdown.extensions.codehilite', \
                                             'markdown.extensions.abbr', \
                                             'markdown.extensions.attr_list', \
                                             'markdown.extensions.def_list', \
                                             'markdown.extensions.fenced_code', \
                                             'markdown.extensions.footnotes', \
                                             'markdown.extensions.smart_strong', \
                                             'markdown.extensions.meta', \
                                             'markdown.extensions.nl2br', \
                                             'markdown.extensions.tables'])
    return html